import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'core/storage/storage_service.dart';
import 'core/controllers/global_controller.dart';
import 'core/theme/app_theme.dart';
import 'core/constants/app_constants.dart';
import 'config/routes.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // Web平台不需要设置系统UI样式和屏幕方向
  if (!kIsWeb) {
    // 这些代码仅在移动端执行
  }
  
  try {
    // 初始化存储服务
    await StorageService.instance.init();
    
    // 初始化全局控制器
    Get.put(GlobalController());
    
    runApp(const MyApp());
  } catch (e) {
    print('Initialization error: $e');
    // 如果初始化失败，运行简化版本
    runApp(const SimpleFallbackApp());
  }
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (context, child) {
        return GetMaterialApp(
          title: AppConstants.appName,
          
          // 路由配置
          initialRoute: AppRoutes.getInitialRoute(),
          getPages: AppRoutes.pages,
          unknownRoute: AppRoutes.unknownRoute,
          
          // 主题配置
          theme: AppTheme.lightTheme,
          darkTheme: AppTheme.darkTheme,
          themeMode: ThemeMode.system,
          
          // 语言配置
          locale: const Locale('zh', 'CN'),
          fallbackLocale: const Locale('en', 'US'),
          
          // 调试配置
          debugShowCheckedModeBanner: false,
          
          // 路由观察者
          navigatorObservers: [],
          
          // 全局中间件
          routingCallback: (routing) {
            // 可以在这里添加全局路由回调逻辑
          },
          
          // 错误处理
          builder: (context, widget) {
            // Web平台的字体处理
            if (kIsWeb) {
              return widget!;
            }
            // 移动端处理字体大小不跟随系统设置
            return MediaQuery(
              data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
              child: widget!,
            );
          },
        );
      },
    );
  }
}

// 简化的回退应用（如果主应用初始化失败）
class SimpleFallbackApp extends StatelessWidget {
  const SimpleFallbackApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Web App',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const FallbackHomePage(),
    );
  }
}

class FallbackHomePage extends StatelessWidget {
  const FallbackHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter Web App'),
      ),
      body: const Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              'Web应用正在运行',
              style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
            ),
            SizedBox(height: 20),
            Text('这是一个简化的页面，用于测试Web兼容性'),
          ],
        ),
      ),
    );
  }
}